package logic

import (
	"context"
	"errors"
	"fmt"
	"gozeroSingle/internal/model"
	"gozeroSingle/internal/svc"
	"gozeroSingle/internal/types"
	"gozeroSingle/utils"

	"github.com/zeromicro/go-zero/core/logx"
)

/*
 +----------------------------------------------------------------------
 + Title        : UserLogic
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2025-08-08
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 用户逻辑
 +----------------------------------------------------------------------
*/

type UserLogic struct {
	BaseLogic
}

/**
 * 创建用户逻辑实例
 *
 * @param context.Context ctx ----------------------必传
 * @param *svc.ServiceContext svcCtx ---------------必传
 * @return *UserLogic
 * @author huwl
 * @date 2025/10/14 12:54:03
 */
func NewUserLogicInstance(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogic {
	return &UserLogic{
		BaseLogic{
			Ctx:    ctx,
			SvcCtx: svcCtx,
			Logger: logx.WithContext(ctx),
		},
	}
}

/**
 * 注册
 *
 * @param map[string]interface{} params ------------必传
 * @return error
 * @author huwl
 * @date 2025/10/14 12:54:03
 */
func (logic *UserLogic) Register(params map[string]interface{}) error {

	// 判断用户是否存在
	total, err := logic.SvcCtx.UserModel.GetResultsByTotal(logic.Ctx, map[string]interface{}{
		"username": params["username"].(string),
	})
	if err != nil {
		return err
	}
	fmt.Println("total =", total)

	return nil
}

/**
 * 登录
 *
 * @param map[string]interface{} params ------------必传
 * @retrue *models.User, error
 * @author huwl
 */
func (logic *UserLogic) Login(params map[string]interface{}) (*model.MallUser, error) {
	user, err := logic.SvcCtx.UserModel.FindOneByUsername(logic.Ctx, params["username"].(string))
	if err != nil {
		return nil, err
	}

	if user.Status != 1 && !utils.CompareBcrypt(user.Password, params["password"].(string)) {
		return user, errors.New("用户名或者密码错误")
	}

	return user, nil
}

/**
 * 列表
 *
 * @param map[string]interface{} params ------------必传
 * @param int page ---------------------------------必传
 * @param int pageSize -----------------------------必传
 * @param string sortField -------------------------必传
 * @param string sortMode --------------------------必传
 * @retrue []*model.MallUser, int64, error
 * @author huwl
 */
func (logic *UserLogic) List(params map[string]interface{}, page uint32, pageSize int8, order string) ([]*model.MallUser, int64, error) {
	// 获取用户数量
	total, err := logic.SvcCtx.UserModel.GetResultsByTotal(logic.Ctx, params)
	if err != nil {
		return nil, 0, err
	}

	// 获取用户数据
	var users []*model.MallUser
	if total > 0 {
		err = logic.SvcCtx.UserModel.GetResultsByParams(logic.Ctx, &users, params, page, pageSize, order)
		if err != nil {
			return nil, 0, err
		}
	}

	return users, total, nil
}

/**
 * 数量
 *
 * @param map[string]interface{} params ------------必传
 * @retrue int64, error
 * @author huwl
 */
func (logic *UserLogic) Total(params map[string]interface{}) (int64, error) {
	// 获取用户数量
	total, err := logic.SvcCtx.UserModel.GetResultsByTotal(logic.Ctx, params)
	if err != nil {
		return 0, err
	}
	return total, nil
}

/**
 * 数量
 *
 * @param []map[string]interface{} params ------------必传
 * @retrue int64, error
 * @author huwl
 */
func (logic *UserLogic) BatchAdd(params []map[string]interface{}) (int64, error) {
	mu := []interface{}{}
	for _, value := range params {
		mu = append(mu, model.MallUser{
			Username:   value["username"].(string),
			Mobile:     value["mobile"].(string),
			CreateTime: types.Now(),
		})
	}
	return logic.SvcCtx.UserModel.BatchAdd(logic.Ctx, mu)
}
